use core::net;
use std::{
    net::{AddrParseError, SocketAddr},
    str::FromStr,
};

use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {
    #[error("invalid protocol '{0}'")]
    InvalidProto(String),

    #[error("invalid addr '{0}': {1}")]
    InvalidAddr(String, AddrParseError),
}

/// Decoded address format.
pub enum Address {
    Sock(SocketAddr),
}

impl FromStr for Address {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let sa = net::SocketAddr::from_str(s).map_err(|e| Error::InvalidAddr(s.to_owned(), e))?;
        Ok(Address::Sock(sa))
    }
}
